//
//  FirstTemplateVC.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FirstTemplateVC.h"

@interface FirstTemplateVC ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSArray *dataArr;
// 标题中的名字 描述 图片
@property (nonatomic, retain) NSString *topic_name;
@property (nonatomic, retain) NSString *topic_des;
@property (nonatomic, retain) NSString *host_pic;
@property (nonatomic, assign) CGFloat heightOfDes;
@property (nonatomic, retain) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) NSInteger n;
@property (nonatomic, retain) UICollectionReusableView *header;
@end

@implementation FirstTemplateVC
// 页面即将出现 将tabBar显示
- (void)viewWillAppear:(BOOL)animated
{
    // tabBar显示
    self.tabBarController.tabBar.hidden = YES;
}
// 页面即将消失 将tabBar隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    // tabBar隐藏
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.n = 0;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:234 / 255.0 green:86 / 255.0 blue:81 / 255.0 alpha:1.0];
    self.navigationItem.title = @"教程";
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    // 添加数据处理方法
    // 在页面中添加collectionView
    // 定义collectionView的layout
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2 / 0.618);
    _layout.headerReferenceSize = CGSizeMake(100, WIDTH / 2 + self.heightOfDes + 30);
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 注册collectionView的cell重用池
    [_collectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    // 给collectionView添加一个header
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collectionView];
    [self dataHandler];
}
- (void)dataHandler
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?c=Shiji&a=topicInfo&topic_id=%@&vid=15", self.topicId] completion:^(id result) {
        NSArray *arrTemp = result[@"data"][@"goods"];
        self.dataArr = [FirstModel modelHandlerWithArray:arrTemp];
        self.topic_name = result[@"data"][@"topic_name"];
        self.topic_des = result[@"data"][@"topic_des"];
        self.host_pic = result[@"data"][@"host_pic"];
        self.heightOfDes = [HeightAdaption heightWithText:self.topic_des Size:16 Width:WIDTH - 20];
        [_collectionView reloadData];
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    self.header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    _layout.headerReferenceSize = CGSizeMake(100, WIDTH / 2 + self.heightOfDes + 30);
    _header.backgroundColor = [UIColor whiteColor];
    CGFloat h = [HeightAdaption heightWithText:self.topic_des Size:16 Width:WIDTH - 20];
    CGFloat heightOfTitle = [HeightAdaption heightWithText:self.topic_name Size:20 Width:WIDTH - 20];
    if (self.n < 2) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 2 + h + 20)];
        UIImageView *headerImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 2)];
        [headerImg sd_setImageWithURL:[NSURL URLWithString:self.host_pic]];
        headerImg.backgroundColor = [UIColor yellowColor];
        [headerView addSubview:headerImg];
        UILabel *headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 20, heightOfTitle)];
        headerTitle.font = [UIFont systemFontOfSize:20];
        headerTitle.center = headerImg.center;
        headerTitle.text = self.topic_name;
        headerTitle.textColor = [UIColor whiteColor];
        headerTitle.numberOfLines = 0;
        [headerImg addSubview:headerTitle];
        UILabel *headerDes = [[UILabel alloc] initWithFrame:CGRectMake(10, WIDTH / 2 + 10, WIDTH - 20, h)];
        headerDes.text = self.topic_des;
        headerDes.font = [UIFont systemFontOfSize:16];
        headerDes.numberOfLines = 0;
        [headerView addSubview:headerDes];
        [_header addSubview:headerView];
    }
    self.n++;
    return _header;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FirstModel *m = _dataArr[indexPath.item];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.picurl]];
    cell.titleLabel.text = m.title;
    cell.priceLabel.text = [NSString stringWithFormat:@"¥%@", m.yh_price];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstModel *m = _dataArr[indexPath.item];
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.url = [@"http://h5.m.taobao.com/cm/snap/index.html?u_channel=1-23259936&umpChannel=1-23259936&exParams=%7B%22umpChannel%22:%221-23259936%22,%22u_channel%22:%221-23259936%22,%22referer%22:%22showTaoKeItemDetail%22%7D&ttid=2014_0_23259936@baichuan_ios_1.8.3&id=" stringByAppendingString:m.num_iid];
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
